const { Controller } = require('egg');
const { searchQuery } = require('../utils/tools');
const { mysqlName } = require("../utils/config")
const { SYTEM_USER } = mysqlName
class Login extends Controller {
    async login() {
        //登录
        const { ctx, app } = this;
        const params = ctx.request.body || {};
        const tableName = SYTEM_USER
        console.log(params, 'params')
        const { sqlQuery } = searchQuery(tableName, params, 1, 100)
        const data = await ctx.app.mysql.query(sqlQuery)
        if (!data.length) {
            ctx.body = {
                status: 200,
                msg: "账号或密码不对,请检查"
            }
        } else {
            const token = app.jwt.sign({
                username: params.username,
                password: params.password
            }, app.config.jwt.secret, {
                expiresIn: '24h' // '1d'、'24h'、60，其中数值单位为秒
            });
            ctx.body = {
                status: 200,
                token: token,
            };
        }

    }
}

module.exports = Login;
